package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.common.Result;
import com.example.demo.entity.Course;
import com.example.demo.entity.User;
import com.example.demo.mapper.CourseMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Resource
    CourseMapper courseMapper;

    @PostMapping("/add")
    public Result<?> createNewCourse(@RequestBody Course newCourse){
        QueryWrapper<Course> addWrapper=new QueryWrapper<>();
        addWrapper.eq("teacher",newCourse.getTeacher());
        addWrapper.eq("name",newCourse.getName());
        Course res = courseMapper.selectOne(addWrapper);
        if (res != null) {
            return Result.error("-1", "课程重复！");
        }else{
            String predate=newCourse.getTime();
            boolean isright= predate.length() % 4 == 3;
            for(int i=0;i<predate.length();i++){
                char curr=predate.charAt(i);
                switch(i%4){
                    case 0-> {
                        if(curr!='1'&&curr!='2'&&curr!='3'&&curr!='4'&&curr!='5'&&curr!='6'&&curr!='7')
                            isright=false;
                    }
                    case 1-> {
                        if(curr!='-')
                            isright=false;
                    }
                    case 2-> {
                        if(curr!='1'&&curr!='2'&&curr!='3'&&curr!='4'&&curr!='5'&&curr!='6')
                            isright=false;
                    }
                    case 3-> {
                        if(curr!=',')
                            isright=false;
                    }
                }
            }
            if(!isright){
                return Result.error("-2","上课时间设置错误！");
            }else{
            courseMapper.insert(newCourse);
            return Result.success();
            }
        }
    }

    @PostMapping("/deleteCourse")
    public Result<?> deleteCourse(@RequestBody Course targetCourse){
        QueryWrapper<Course> deleteWrapper=new QueryWrapper<Course>();
        deleteWrapper.eq("name",targetCourse.getName());
        deleteWrapper.eq("id",targetCourse.getId());
        Course res = courseMapper.selectOne(deleteWrapper);
        if (res == null) {
            return Result.error("-1", "未创建该课程！");
        }else{
        courseMapper.delete(deleteWrapper);
        return Result.success();
        }
    }

    @PostMapping("/getCourse")
    public Result<List<Course>> getCourse(@RequestBody User currentUser){
        QueryWrapper<Course> courseWrapper=new QueryWrapper<Course>();
        courseWrapper.eq("teacher",currentUser.getName());
        List<Course> resList =courseMapper.selectList(courseWrapper);
        return Result.success(resList);
    }

    @PutMapping("/upload")
    public Result<?> updateCourse(@RequestBody Course targetCourse){
        String predate=targetCourse.getTime();
        boolean isright= predate.length() % 4 == 3;
        for(int i=0;i<predate.length();i++){
            char curr=predate.charAt(i);
            switch(i%4){
                case 0-> {
                    if(curr!='1'&&curr!='2'&&curr!='3'&&curr!='4'&&curr!='5'&&curr!='6'&&curr!='7')
                        isright=false;
                }
                case 1-> {
                    if(curr!='-')
                        isright=false;
                }
                case 2-> {
                    if(curr!='1'&&curr!='2'&&curr!='3'&&curr!='4'&&curr!='5'&&curr!='6')
                        isright=false;
                }
                case 3-> {
                    if(curr!=',')
                        isright=false;
                }
            }
        }
        if(!isright){
            return Result.error("-1","上课时间设置错误！");
        }else{
            courseMapper.updateById(targetCourse);
            return  Result.success();
        }
    }

    @PostMapping("/getAllCourse")
    public Result<?> getAllCourse(){
        QueryWrapper<Course> queryWrapper=new QueryWrapper<>();
        queryWrapper.isNotNull("id");
        List<Course> resList=courseMapper.selectList(queryWrapper);
        return Result.success(resList);
    }

    @PostMapping("/queryDate")
    public Result<?> queryCourseDate(@RequestBody Course targetCourse){
        QueryWrapper<Course> addWrapper=new QueryWrapper<>(targetCourse);
        Course res = courseMapper.selectOne(addWrapper);
        Date currdate=new Date();
        Date limitdate=new Date();
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");
        try {
            limitdate=ft.parse(res.getLimitdate());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(currdate.after(limitdate)){
            return Result.error("-1","超过选课时间");
        }else{
            return Result.success();
        }
    }

}
